﻿using SMS.cntr;
using SMS.dao;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS.util;
using System.Resources;

namespace SMS.view
{
    public partial class AddClazzForm : Form
    {
        public AddClazzForm()
        {
            InitializeComponent();
        }
//Quang===================================

        private String clazzID = "";
        private void btnaddClazz_Click(object sender, EventArgs e)
        {
            
            String clazzName = txtClazzName.Text;
            string clazzCapacity = txtClazzCapacity.Text; 
            int blockID =Int32.Parse(cbBlock.SelectedValue.ToString());
            bool result = ClazzCntr.Instance.createClazz(clazzName, clazzCapacity , blockID);
            if (result) 
            {
                MessageBox.Show("Thêm lớp thành công! ");
                bindGridViewClass();
            }
        }

     

        private void addClazz_Load(object sender, EventArgs e)
        {
            bindCbBlock();
            bindGridViewClass();
            changeLang(I18nUtil.vnLang);
        }

        private void bindGridViewClass()
        {
             
            gridViewClazz.Rows.Clear();
            try
            {                
                DataTable table = ClazzCntr.Instance.findClazzByBlock(Int32.Parse(cbBlock.SelectedValue.ToString()));
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    gridViewClazz.Rows.Add(i + 1, table.Rows[i]["cla_id"].ToString(), table.Rows[i]["cla_name"].ToString(), table.Rows[i]["cla_counted"].ToString(), table.Rows[i]["cla_capacity"].ToString());
                    gridViewClazz.Rows[gridViewClazz.RowCount - 1].Tag = table.Rows[i];
                }
                gridViewClazz.ClearSelection();
            }
            catch (Exception ex) {  }
        
        }

        private void bindCbBlock()
        {
            cbBlock.DataSource = BlockCntr.Instance.listAllBlockCounted();
            cbBlock.DisplayMember = "blo_name";
            cbBlock.ValueMember = "blo_id";            
        }

        private void cbBlock_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindGridViewClass();
        }

        private void gridViewClazz_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < gridViewClazz.SelectedRows.Count; i++)
            {
                int id = Int32.Parse(gridViewClazz.SelectedRows[i].Cells[1].Value.ToString());
                txtClazzName.Text = ClazzDao.Instance.findById(id)[2].ToString();
                txtClazzCapacity.Text = ClazzDao.Instance.findById(id)[3].ToString();
                clazzID = ClazzDao.Instance.findById(id)[0].ToString();
                //int currentCapacity = ClazzCntr.Instance.countStudentInCLazz(int.Parse(clazzID));
                //MessageBox.Show(currentCapacity.ToString());
            }
        }

        private void btnUpdateClazz_Click(object sender, EventArgs e)
        {
            if (clazzID == "") { MessageBox.Show("Mời chọn lớp cần cập nhật"); }
            else 
            {
            string clazzName = txtClazzName.Text.ToString();
            string clazzCapacity = txtClazzCapacity.Text.ToString();
            ClazzCntr.Instance.update(int.Parse(clazzID), clazzName, clazzCapacity);
            bindGridViewClass(); 
            }
        }

        private void btnDeleteClazz_Click(object sender, EventArgs e)
        {
            ClazzCntr.Instance.delete(int.Parse(clazzID));
            bindGridViewClass();
        }

        public void changeLang(String lang)
        {
            I18nUtil.init(lang, new ResourceManager(I18nUtil.resourcePath, typeof(AddClazzForm).Assembly));

            this.Text = I18nUtil.getString("createClass");
            label1.Text = I18nUtil.getString("lblClassName");
            label3.Text = I18nUtil.getString("lblBlock");
            label2.Text = I18nUtil.getString("lblCapacity");
            btnaddClazz.Text = I18nUtil.getString("btnAdd");
            btnUpdateClazz.Text = I18nUtil.getString("btnUpdate");
            btnDeleteClazz.Text = I18nUtil.getString("btnDelete");
            gridViewClazz.Columns[0].HeaderText = I18nUtil.getString("no");
            gridViewClazz.Columns[2].HeaderText = I18nUtil.getString("lblClassName");
            gridViewClazz.Columns[3].HeaderText = I18nUtil.getString("currentCapacity");
            gridViewClazz.Columns[4].HeaderText = I18nUtil.getString("maxCapacity");
        }


       //===================================================
    }
}
